home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.onyx.net!claymoor
- From: Adam.Morris@octacon.co.uk (Adam Morris)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Simple question about constructors
- Date: Wed, 06 Mar 96 17:59:43 GMT
- Organization: Octacon Ltd
- Message-ID: <4hk29c$n0p@mulgave.octacon.co.uk>
- References: <4ha7d2$quc@cloner4.netcom.com>
- NNTP-Posting-Host: claymoor.onyx.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4ha7d2$quc@cloner4.netcom.com>, stefmit@ix.netcom.com wrote:
- >newer source, I came across a syntax in the form:
- >class_name :: class_name (arguments) : data_member1 (argument1), data_member2
- >(argument2) {}
- >The portion I don't know the meaning of (though I assume is kind of
- >intialization list, that would've appeared otherwise within {}, as far as I
- >learned from my book), is the portion after ":".
- >I would kindly request some elaboration on this, especially concerning a
- >statement in the source where I found this, similar to: "this is the only way
- >the initialization can be done in some cases (!!!)". Does this mean I am not
- >always able to define a constructor the way I've learned so far (in its {}
- >body)?
-
- Yep it's an initialisation list... It's always best to initalise everything
- possible like that than in the body of the constructor. as far as MUST be
- done... then const and reference members can only be initialised and never
- assigned. it's also preferable for efficiency reasons. initialisation of a
- string calls a string constructor once, assignment calls two constructors...
- one to construct the string, and one to assign to it...
-
- For a better discussion of why hunt down Effective C++ by Scott Meyers and
- read Item 12...
-
- I hope this has explained (at least roughly) what you wanted to know.
-
- Adam.
-